package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.PatrolViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import kotlinx.android.synthetic.main.layout_title_search_img.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColor
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class PatrolPeopleActivity : BaseActivity() {

    private var mKey = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patrol_people)
        setToolbarVisibility(false)
        initTitle()

        swipe_refresh.isRefreshing = true
        getData(pageNum)

        observe("patrol_add") { updateList() }
    }

    override fun getViewModel() = getViewModel<PatrolViewModel>()

    override fun initTitle(title: String, name: String) {
        search_edit.hint = "请输入姓名或身份证号"
        empty_hint.text = "暂无相关人口巡检信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.loadLinear(baseContext, swipe_refresh) {
            if (!isLoadingMore) {
                isLoadingMore = true
                getData(pageNum)
            }
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_patrol_list1) { data, injector ->
                injector.text(R.id.item_patrol_name, data.name)
                    .text(R.id.item_patrol_tel, data.housePhone)
                    .text(R.id.item_patrol_id, data.identity)
                    .text(R.id.item_patrol_time, data.submitDate.toFormatString())
                    .text(R.id.item_patrol_memo, data.remark)
                    .with<TextView>(R.id.item_patrol_result) {
                        when (data.tourResult) {
                            "1" -> {
                                it.text = "正常"
                                it.textColor = getColorEx(R.color.black)
                            }
                            "2" -> {
                                it.text = "异常"
                                it.textColor = getColorEx(R.color.red)
                            }
                        }
                    }
                    .clicked(R.id.item_patrol) {
                        if (data.bisId.isNullOrEmpty()) return@clicked

                        startActivity<PatrolPeopleDetialActivity>(
                            "type" to data.type,
                            "id" to data.bisId
                        )
                    }
            }
            .attachTo(recycle_list)

        search_right.oneClick { startActivity<PatrolPeopleScanActivity>() }
        search_close.oneClick { search_edit.setText("") }
        search_edit.addTextChangedListener(this@PatrolPeopleActivity)
        search_edit.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                hideSoftInput() //隐藏软键盘

                if (search_edit.text.isBlank()) {
                    toast("请输入关键字")
                } else {
                    mKey = search_edit.trimString()
                    updateList()
                }
            }
            return@setOnEditorActionListener false
        }

    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getTourList(index, 1, getString("userId"), mKey)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (count(it) > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

    private fun updateList() {
        swipe_refresh.isRefreshing = true

        empty_view.gone()
        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        pageNum = 1
        getData(pageNum)
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        search_close.visibility = if (s.isEmpty()) View.GONE else View.VISIBLE
        if (s.isEmpty() && mKey.isNotEmpty()) {
            mKey = ""
            updateList()
        }
    }

}
